#define  _KEY_GLOBAL_
#include "config.h"

void keyExecID(enumKeyID keyID,enumKeyStatus keyStatus);

typedef struct _tKEY_ATTRIBUTION_V2
{
	enumKeyID KeyIDShort;
	enumKeyID KeyIDLong;
	U8 DownAttribution;
	U8 ShortAttribution;
	U32 LongAttribution;
	U32 ContinueAttribution;
	U8 UpAttribution;
}tKEY_ATTRIBUTION_V2;

const tKEY_ATTRIBUTION_V2 keyAtt[] =
{
	{KB_1,KB_1_LONG_PRESS,0,1,1200,0,0},
	{KB_2,KB_2_LONG_PRESS,0,1,1200,0,0},
	
	{KB_NP,KB_NP,0},
};

const tKEY_ATTRIBUTION_V2 *getKeyAtt(const tKEY_ATTRIBUTION_V2 *pKeyAttStart,U8 keyID)
{
	const tKEY_ATTRIBUTION_V2 *pKeyAtt = &pKeyAttStart[0];

	if (keyID)
	{
		while (KB_NP != pKeyAtt->KeyIDShort)
		{
			if (keyID == pKeyAtt->KeyIDShort)
			{
				break;
			}
			pKeyAtt++;
		}
	}	

	if (KB_NP == keyID || KB_NP == pKeyAtt->KeyIDShort)
	{
		return NULL;
	}
	else
	{
		return pKeyAtt;
	}
}

void keyPressProc(KEY_ID_PROC *pKeyIDProc,BOOL bDown)
{
	const tKEY_ATTRIBUTION_V2 *pKeyAtt;

	pKeyAtt = getKeyAtt(keyAtt,pKeyIDProc->keyID);

	if (NULL == pKeyAtt || (KB_NP == pKeyAtt->KeyIDShort && KB_NP == pKeyAtt->KeyIDLong))
	{
		pKeyIDProc->keyLongContinueProc = FALSE;
		pKeyIDProc->keyDownTime = 0;
		return;
	}
	
	if (!bDown)
	{
		if (pKeyAtt->UpAttribution)
		{
			keyExecID(pKeyAtt->KeyIDShort,enumKeyStatusRelease);
		}
		if (pKeyAtt->ShortAttribution && !pKeyIDProc->keyLongContinueProc)
		{
			keyExecID(pKeyAtt->KeyIDShort,enumKeyStatusClick);
		}

		pKeyIDProc->keyLongContinueProc = FALSE;
		pKeyIDProc->keyDownTime = 0;
	}
	else
	{
		if (0 == pKeyIDProc->keyDownTime)
		{
			pKeyIDProc->keyDownTime = GetTickCount();
			
			if (pKeyAtt->DownAttribution)
			{
				keyExecID(pKeyAtt->KeyIDShort,enumKeyStatusPress);
			}
			if (pKeyAtt->ShortAttribution && !pKeyAtt->LongAttribution)
			{
				keyExecID(pKeyAtt->KeyIDShort,enumKeyStatusClick);
			}
		}
		else
		{
			if (pKeyIDProc->keyLongContinueProc)
			{
				if (pKeyAtt->ContinueAttribution)
				{
					if (GetTickCount() - pKeyIDProc->keyDownTime >= pKeyAtt->ContinueAttribution)
					{
						pKeyIDProc->keyDownTime = GetTickCount();

						keyExecID(pKeyAtt->KeyIDLong,enumKeyStatusContinue);
					}
				}
			}
			else
			{
				if (pKeyAtt->LongAttribution)
				{
					if (GetTickCount() - pKeyIDProc->keyDownTime >= pKeyAtt->LongAttribution)
					{
						pKeyIDProc->keyLongContinueProc = TRUE;
						pKeyIDProc->keyDownTime = GetTickCount();

						keyExecID(pKeyAtt->KeyIDLong,enumKeyStatusClick);
					}
				}
			}
		}
	}
}
	U16 keyAD;
U8 transADToKey(void)
{

	U8 keyID;
	static U8 keyIDLast;
	static U32 keyCount;
	static U8 keyIDSend = KB_NP;

	keyAD = adcGetKeyValue();
	
	if (keyAD < 2885 && keyAD > 1000)
	{
		keyID = KB_2;
	}
	else if (keyAD < 1000)
	{
		keyID = KB_1;
	}
	else
	{
		keyID = KB_NP;
	}

	if (keyIDLast != keyID)
	{
		keyIDLast = keyID;
		keyCount = 0;
		//keyIDSend = KB_NP;
	}
	else
	{
		keyCount++;
	}
	if (keyCount >= 4)
	{
		keyIDSend = keyIDLast;
	}
	
	return keyIDSend;
}

void KeyScanFreeIdleProc(void)
{
	U8 keyValueNow;
	
	keyValueNow = transADToKey();
	if (keyInfo.keyIDProcAD.keyID != keyValueNow)
	{
		keyPressProc(&keyInfo.keyIDProcAD,FALSE);
		keyInfo.keyIDProcAD.keyID = keyValueNow;
	}
	if (keyInfo.keyIDProcAD.keyID)
	{
		keyPressProc(&keyInfo.keyIDProcAD,TRUE);
	}
}

void keyExecID(enumKeyID keyID,enumKeyStatus keyStatus)
{
	if (enumKeyStatusClick == keyStatus)
	{
		
		if (KB_1 == keyID || KB_2 == keyID)
		{

		}
		else if (KB_1_LONG_PRESS == keyID || KB_2_LONG_PRESS == keyID)
		{

		}
	}
}

void keyInit(void)
{
	memset(&keyInfo,0,sizeof(KEY_INFO));
}

